package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    public void queryUserInfo(User user) {
        if (userMapper.selectByEmailAndPassword(user.getEmail(),null) != null) {
            throw new RuntimeException("账号已被注册");
        }
    }

    public void saveOrUpdate(User user) {
        if (user.getId() == null) {
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("/img/user/bg.jpeg");
            user.setGender(User.GENDER_SECRET);
            user.setPlace("安徽");
            user.setSign("一直奋斗的小蜜蜂");
            userMapper.insert(user);
        }else {
            userMapper.updateByPrimaryKey(user);
        }
    }

    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if (user == null) {
            throw new RuntimeException("账号和密码不匹配");
        }
        return user;
    }

    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.queryForList(qo);
        return new PageInfo<User>(list);
    }

    public User getById(User user) {
        return userMapper.selectByPrimaryKey(user.getId());
    }


}
